package cn.edu.bjfu.practice.practice.teacher.controller;

import cn.edu.bjfu.practice.practice.app.common.response.ErrorFromUser;
import cn.edu.bjfu.practice.practice.app.common.response.Response;
import cn.edu.bjfu.practice.practice.manager.pojo.query.LoginForm;
import cn.edu.bjfu.practice.practice.manager.pojo.query.AddTeacherForm;
import cn.edu.bjfu.practice.practice.manager.pojo.query.ModifyTeacherForm;
import cn.edu.bjfu.practice.practice.manager.service.ManagerService;
import cn.edu.bjfu.practice.practice.teacher.pojo.domain.Student;
import cn.edu.bjfu.practice.practice.teacher.pojo.query.*;
import cn.edu.bjfu.practice.practice.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @Value("${login.password}")
    String password;

    @PostMapping("/teacher/add_course")
    public Response<Object> addCourse(@Validated AddCourseForm form) {
        teacherService.addCourse(form);
        return Response.message();
    }

    @PostMapping("/teacher/delete_course")
    public Response<Object> deleteCourse(Integer id) {
        teacherService.deleteCourse(id);
        return Response.message();
    }

    @PostMapping("/teacher/file_course")
    public Response<Object> fileCourse(@Validated FileCourseForm form) {
        teacherService.fileCourse(form);
        return Response.message();
    }

    @PostMapping("/teacher/select_course")
    public Response<Object> selectCourse(Boolean file, Integer teacher_id) {
        return Response.success(teacherService.selectCourse(file, teacher_id));
    }

    @PostMapping("/teacher/add_student")
    public Response<Object> addStudent(@RequestBody @Validated MultipartFile excel, Integer course_id) {
        try {
            List<Student> students = teacherService.addStudent(excel, course_id);
            return Response.success(students);
        }catch (IOException e) {
            return Response.error(ErrorFromUser.FILE_READ_ERROR);
        }
    }

    @PostMapping("/teacher/select_student")
    public Response<Object> selectStudent(Integer course_id) {
        return Response.success(teacherService.selectStudent(course_id));
    }

    @PostMapping("/teacher/update_student")
    public Response<Object> updateStudent(UpdateStudentForm form) {
        teacherService.updateStudent(form);
        return Response.message();
    }

    @PostMapping("/teacher/delete_student")
    public Response<Object> deleteStudent(Integer id) {
        teacherService.deleteStudent(id);
        return Response.message();
    }

    @PostMapping("/teacher/add_project")
    public Response<Object> addProject(AddProjectForm form) {
        teacherService.addProject(form);
        return Response.message();
    }

    @PostMapping("/teacher/modify_project")
    public Response<Object> modifyProject(ModifyProjectForm form) {
        teacherService.modifyProject(form);
        return Response.message();
    }

    @PostMapping("/teacher/delete_project")
    public Response<Object> deleteProject(Integer id) {
        if(teacherService.deleteProject(id) == 1)
            return Response.message();
        else
            return Response.error(ErrorFromUser.PROJECT_DEL_ERROR);
    }

    @PostMapping("/teacher/select_project")
    public Response<Object> selectProject(Integer id) {
        return Response.success(teacherService.selectProject(id));
    }

    @PostMapping("/teacher/add_team")
    public Response<Object> addTeam(AddTeamForm form) {
        teacherService.addTeam(form);
        return Response.message();
    }

    @PostMapping("/teacher/modify_team")
    public Response<Object> modifyTeam(ModifyTeamForm form) {
        teacherService.modifyTeam(form);
        return Response.message();
    }

    @PostMapping("/teacher/delete_team")
    public Response<Object> deleteTeam(Integer id) {
        if(teacherService.deleteTeam(id) == 1)
            return Response.message();
        else
            return Response.error(ErrorFromUser.TEAM_DEL_ERROR);
    }

    @PostMapping("/teacher/select_team")
    public Response<Object> selectTeam(Integer id) {
        return Response.success(teacherService.selectTeam(id));
    }

    @PostMapping("/teacher/select_task")
    public Response<Object> selectTask(SelectTaskForm form) {
        return Response.success(teacherService.selectTask(form));
    }

    @PostMapping("/teacher/select_team_leader")
    public Response<Object> selectTeamLeader(Integer course_id) {
        return Response.success(teacherService.selectTeamLeader(course_id));
    }

    @PostMapping("/teacher/add_single_stu")
    public Response<Object> addSingleStu(AddStudentForm form) {
        teacherService.addSingleStu(form);
        return Response.message();
    }
}
